package com.fafa.yygh.user.api;

import com.fafa.yygh.common.result.Result;
import com.fafa.yygh.common.utils.AuthContextHolder;
import com.fafa.yygh.model.user.UserInfo;
import com.fafa.yygh.user.service.UserInfoService;
import com.fafa.yygh.vo.user.LoginVo;
import com.fafa.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Sire
 * @version 1.0
 * @date 2022-02-18 16:38
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    public Result loginUser(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    @ApiOperation(value = "用户认证接口")
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo,
                           HttpServletRequest request) {
        // 传递两个参数，第一个参数用户id，第二个参数 认证数据的VO对象
        // 用户id使用工具类获取，本质上是在 request 中获得
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        // 解释：这里为啥要用request，而不是直接传过来id呢
        // 因为用户信息登录后，信息会存到token中，直接传cookie到后端即可【可以理解为传token】
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
